// Log.cpp: implementation of the CLog class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Log.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CLog::CLog():
	lErrTime_(0),
	bErrCreate_(true)
{
	lTime_ = 0;
	bCreate_ = true;
	sPre_ = "Log";
	sFileName_ = "Log.txt";
	if (!CreateDirectory("Log", NULL))
	{
		TRACE("Couldn't create new directory.");
	}
}
CLog::CLog(CString sPre):
	lErrTime_(0),
	bErrCreate_(true)
{
	sPre_ = sPre;
	lTime_ = 0;
	bCreate_ = true;
	sFileName_ = "Log.txt";
	if (!CreateDirectory("Log", NULL))
	{
		TRACE("Couldn't create new directory.");
	}
}

CLog::~CLog()
{
}

CLog * CLog::m_pInstance = NULL;

CLog * CLog::Instance()
{
	if (m_pInstance == NULL)
	{
		m_pInstance = new CLog;
	}

	return m_pInstance;
}

void CLog::DeleteInstance()
{
	if (m_pInstance) delete m_pInstance;

	m_pInstance = NULL;
}
bool CLog::WriteDataToFile(CString sFileName, CString sDataStr, bool bCreate) const
{
	//return false;

	int size = sDataStr.GetLength();
	if(size<=0)
		return false;
	bool bOpen = false;
	CFile * pFile = new CFile;

	if(bCreate){
		bOpen = pFile->Open("Log\\"+sFileName, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyNone);
	}else{
		bOpen = pFile->Open("Log\\"+sFileName, CFile::modeWrite | CFile::shareDenyNone) || pFile->Open("Log\\"+sFileName, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyNone);
	}
	if(bOpen){
		pFile->Seek(0, CFile::end);

		pFile->Write((void *)sDataStr.GetBuffer(size) , size);

		pFile->Close();
	}

	delete pFile;
	return true;
}
bool CLog::WriteDataToFile( CString sDataStr )
{
	//return false;

	Guard guard(&m_mutexLog);
	int size = sDataStr.GetLength();

	CFile * pFile = new CFile;

	if (pFile->Open("Log\\"+sFileName_, CFile::modeWrite | CFile::shareDenyNone) || pFile->Open("Log\\"+sFileName_, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyNone))
	{
		pFile->Seek(0, CFile::end);

		pFile->Write((void *)sDataStr.GetBuffer(size) , size);

		pFile->Close();
	}

	delete pFile;

	/*
	CFile cFile ;
	CFileException ex;
	int size = sDataStr.GetLength();

	if(size<=0)
	return false;
	bool bOpen = false;
	if(bCreate_){
	bOpen = cFile.Open(sFileName_, CFile::modeWrite |CFile::modeCreate |CFile::shareDenyNone, &ex);
	}else{
	bool bExist = cFile.Open(sFileName_  ,   CFile::modeRead   |   CFile::shareDenyNone   ,   NULL);
	if(!bExist)
	bOpen = cFile.Open(sFileName_, CFile::modeWrite |CFile::modeCreate |CFile::shareDenyNone, &ex);
	else
	bOpen = cFile.Open(sFileName_, CFile::modeReadWrite |CFile::shareDenyNone, &ex);
	}
	if(!bOpen)
	{
	TCHAR szError[1024];
	ex.GetErrorMessage(szError, 1024);
	AfxMessageBox(szError, MB_OK);
	return false;
	}else{
	try{
	bCreate_ = false;
	char *pbuffer;
	pbuffer = new char[size+1];
	sprintf(pbuffer, "%s",sDataStr);
	long pos = 0;
	cFile.SeekToEnd();
	cFile.Write(pbuffer,size);
	cFile.Close();
	delete [] pbuffer;
	}catch(CFileException *e){
	e->Delete();
	}
	}
	*/
	return true;
}

bool CLog::Log(CString sLogStr)
{
	CTime tm =  CTime::GetCurrentTime();
	long lTm = ((tm.GetMonth()*31+tm.GetDay())*24+tm.GetHour())*60+tm.GetMinute();
	if(lTm > lTime_+10){
		bCreate_ = true;
		lTime_ = lTm;
		sFileName_ = sPre_+tm.Format(_T("%Y%m%d_%H"))+".txt";
	}else{
		bCreate_ = false;
	}
	CString sEvent =tm.Format(_T("%Y-%m-%d %H:%M:%S")) +" " + sLogStr +"\r\n";

	bool bRet =  WriteDataToFile(sEvent );
	sEvent = "";
	return bRet;
}

bool CLog::Err(CString sErrStr) const
{
	CTime tm =  CTime::GetCurrentTime();

	CString	sFileName = "Err_"+tm.Format(_T("%Y%m%d_%H"))+".txt";
	CString sEvent =tm.Format(_T("%Y-%m-%d %H:%M:%S")) + " " + sErrStr +"\r\n";

	bool bRet = WriteDataToFile(sFileName, sEvent, false );
	sEvent = "";
	sFileName = "";
	return bRet;
}